package com.linkage.book.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.linkage.book.pojo.Book;
import com.linkage.book.service.BookService;

/**
 * 
 * @author LuoYunHui
 *
 */
@Controller
@RequestMapping("/book")
public class BookController {
	@Autowired
	private BookService bookService;
	
	@RequestMapping(value = "/getBookByName", method = RequestMethod.POST)
	public String getBookByName(@RequestParam("bookName") String bookName, Model model) {
		Book book = this.bookService.loadByBookName(bookName);
        model.addAttribute("book", book);

        return "menu/body";
	}
	
	@RequestMapping(value = "/getBooksByBookName", method = RequestMethod.POST)
	public String getBooksByBookName(@RequestParam("bookName") String bookName, Model model) {
		List<Book> books = this.bookService.getBooksByBookName(bookName);
		model.addAttribute("books", books);
		
		return "menu/body";
	}
	
	@RequestMapping(value = "/getBooksByCombination", method = RequestMethod.POST)
	public String getBooksByCombination(@RequestParam("bookName") String bookName,  @RequestParam("author") String author,
			@RequestParam("press") String press, Model model) {
		List<Book> books = this.bookService.getBooksByCombination(bookName, author, press);
		model.addAttribute("books", books);
		
		return "menu/body";
	}
	
	@RequestMapping(value = "/getBooksByCategoryId/{categoryId}", method = RequestMethod.GET)
	public String getBooksByCategoryId(@PathVariable("categoryId") int categoryId, Model model) {
		List<Book> books = this.bookService.getBooksByCategoryId(categoryId);
		model.addAttribute("books", books);
		
		return "menu/body";
	}
	
	
	@RequestMapping(value = "/getBookDetail/{bookId}", method = RequestMethod.GET)
	public String getBookDetail(Model model, @PathVariable("bookId") int bookId) {
		Book book = this.bookService.loadOne(bookId);
		model.addAttribute("book", book);
		
		return "menu/bookDetail";
	}
	
}
